﻿using System;
using System.Collections.Generic;
using System.Text;
using Galaxy.ModuleInterface;
using System.Windows.Forms;
using Galaxy.ModuleInterface.Events;
using Galaxy.ModuleInterface.Messages;

namespace Galaxy.ModuleInspector
{
    public partial class GalaxyModuleInspector : GalaxyModule, IGalaxyModule
    {
        /* ---------------------------------------------------------------------- */

        private bool fEnable;
        private bool fInitialized;

        private ModuleInspector_Form MainForm;

        /* ---------------------------------------------------------------------- */

        public override bool Enable
        {
            get { return fEnable; }
            set { if(value) Start(); else Stop(); }
        }

        public override bool Initialized
        {
            get { return fInitialized; }
        }

        /* ---------------------------------------------------------------------- */

        public GalaxyModuleInspector() : base("Инспектор модулей Галактики") { }

        public GalaxyModuleInspector(string Name) : base(Name) { }

        /* ---------------------------------------------------------------------- */

        private delegate void RunDelegate(Form Form);

        [STAThread]
        public override void Start()
        {               
            if(Enable) return;
            if(!Initialized) Initialize();
            RunDelegate Run = Application.Run;
            Run.BeginInvoke(MainForm, OnInspectorFormClosed, MainForm);
            fEnable = true;

#if DEBUG
            Console.WriteLine("Инспектор модулей Галактики запущен");
#endif
        }

        private delegate void NonArgumentDelegate();
        public override void Stop()
        {
            if(!Enable) return;
            if(!Initialized) return;
            fEnable = false;
            if(MainForm != null)
            {
                MainForm.ModuleInspector = null;
                MainForm.CloseForm();
            }

#if DEBUG
            Console.WriteLine("Инспектор модулей Галактики остановлен");
#endif
        }

        public override void Initialize()
        {
            if(MainForm != null) MainForm.CloseForm();

            MainForm = new ModuleInspector_Form();
            MainForm.ModuleInspector = this;
            fInitialized = true;


#if DEBUG
            Console.WriteLine("Инспектор модулей Галактики проинициализирован");
#endif
        }

        /* ---------------------------------------------------------------------- */

        public override void Connect(IGalaxyModule Module)
        {
            base.Connect(Module);
            InvokeOnModuleConnected(Module);
        }

        public override void Disconnect(IGalaxyModule Module)
        {
            base.Disconnect(Module);
            InvokeOnModuleDisconnected(Module);
        }

        public override void ReceiveMessage(IGalaxyModule Module, NewMessageEventArgs Arg)
        {
            base.ReceiveMessage(Module, Arg);
            MainForm.ShowMessage(Arg.Message);
        }

        /* ---------------------------------------------------------------------- */

        private void OnInspectorFormClosed(IAsyncResult AsyncResult)
        {
            if(AsyncResult == null) return;
            if(AsyncResult.AsyncState == MainForm && Enable) Stop();
        }

        public void SendMessage(IGalaxyModuleMessage Message)
        {
            if(Message == null) return;
            InvokeNewMessage(Message);
        }
    }
}
